package com.bootdo.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.system.domain.TbWeixinconfig;
import com.bootdo.system.domain.TbWeixinconfigExample;
import com.bootdo.system.domain.TbWeixinconfigExample.Criteria;
import com.bootdo.system.mapper.TbWeixinconfigMapper;
import com.bootdo.system.service.WxConfigService;

@Service
public class WxConfigServiceImpl implements WxConfigService{

	
	@Autowired
    private TbWeixinconfigMapper sm;
	
	@Override
	public List<TbWeixinconfig> list(Integer userId,List<Integer> userIds) {
		TbWeixinconfigExample tbWeixinconfigExample = new TbWeixinconfigExample();
		Criteria createCriteria = tbWeixinconfigExample.createCriteria();
		if(userIds != null && userIds.size() > 0) {
			createCriteria.andAdminIdIn(userIds);
		}
		if(userId != null) {
			createCriteria.andAdminIdEqualTo(userId);
		}
		return sm.selectByExample(tbWeixinconfigExample);
	}

	@Override
	public int insert2update(TbWeixinconfig table) {
		int res=0;
		if(table != null) {
			Integer id = table.getId();
			if(id != null) {
				table.setUpdatetime(DateUtils.format(new Date()));
				res=sm.updateByPrimaryKeySelective(table);
			}else {
				table.setCreatetime(DateUtils.format(new Date()));
				res=sm.insertSelective(table);
			}
		}
		return res;
	}

	@Override
	public TbWeixinconfig getById(Integer id) {
		return sm.selectByPrimaryKey(id);
	}

	@Override
	public TbWeixinconfig selectByAppId(String appid) {
		TbWeixinconfigExample tbWeixinconfigExample = new TbWeixinconfigExample();
		Criteria createCriteria = tbWeixinconfigExample.createCriteria();
		createCriteria.andAppidEqualTo(appid);
		List<TbWeixinconfig> selectByExample = sm.selectByExample(tbWeixinconfigExample);
		if(selectByExample.size() > 0) {
			return selectByExample.get(0);
		}
		return null;
	}

	@Override
	public int remove(Integer id) {
		return sm.deleteByPrimaryKey(id);
	}

	@Override
	public int batchremove(Integer[] wxIds) {
		int res=0;
		if(wxIds.length > 0) {
			for (int i = 0; i < wxIds.length; i++) {
				res=sm.deleteByPrimaryKey(wxIds[i]);
			}
		}
		return res;
	}

	@Override
	public TbWeixinconfig selectByUserId(Integer userId) {
		TbWeixinconfigExample tbWeixinconfigExample = new TbWeixinconfigExample();
		Criteria createCriteria = tbWeixinconfigExample.createCriteria();
		createCriteria.andAdminIdEqualTo(userId);
		List<TbWeixinconfig> selectByExample = sm.selectByExample(tbWeixinconfigExample);
		if(selectByExample.size() > 0) {
			return selectByExample.get(0);
		}
		return null;
	}

}
